package edu.flying.panda.taskmanager;


import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.app.AlertDialog;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import java.util.ArrayList;

import android.content.Context;
import com.google.android.maps.ItemizedOverlay;

public class Map extends MapActivity {
    /** Called when the activity is first created. */
    MapView map;
    long start;
    long stop;
    int x,y;
    GeoPoint touchedPoint;
    String display;
    Drawable d;
    List<Overlay> overlayList;
    String location;

    
    
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map_screen);
        map = (MapView) findViewById (R.id.map);
        map.setBuiltInZoomControls(true);
        map.displayZoomControls(true);
        
        Bundle extras = getIntent().getExtras();
		
		location = extras.getString("location");
        
        
        List<Overlay> mapOverlays = map.getOverlays();
        Drawable drawable = this.getResources().getDrawable(R.drawable.ic_launcher);
        OnTop itemizedOverlay = new OnTop(drawable, this);
        
        
        
        
        
        
        
        Geocoder geocoder = new Geocoder(this, Locale.getDefault());
        List<Address> addresses;
        double longitude;
        double latitude;
        GeoPoint point;
        
        
		try {
			addresses = geocoder.getFromLocationName(location, 1);
			Address address = addresses.get(0);
	        longitude = address.getLongitude();
	        latitude = address.getLatitude();
	        
	        Log.d("long", Double.toString(longitude));
	        Log.d("lat", Double.toString(latitude));
	        Log.d("longint", Integer.toString( (int)longitude));
	        Log.d("latint", Integer.toString( (int)latitude));
	        
	        point = new GeoPoint((int)(latitude*1000000), (int)(longitude*1000000));
	        OverlayItem overlayitem = 
	             new OverlayItem(point, "please", "WORK!");
	        
	        itemizedOverlay.addOverlay(overlayitem);
	        mapOverlays.add(itemizedOverlay);
	        
	        MapController mapController = map.getController();
	        
	        mapController.animateTo(point);
	        mapController.setZoom(6);
	        
	        
		} catch (IOException e) {
			
			e.printStackTrace();
		}
        
		
        
        
        
        
    }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }
    
    

    

    public class OnTop extends ItemizedOverlay<OverlayItem> {
       
      

	private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
       
       private Context context;
       
       public OnTop(Drawable defaultMarker) {
            super(boundCenterBottom(defaultMarker));
       }
       
       public OnTop(Drawable defaultMarker, Context context) {
            this(defaultMarker);
            this.context = context;
       }

       @Override
       protected OverlayItem createItem(int i) {
          return mapOverlays.get(i);
       }

       @Override
       public int size() {
          return mapOverlays.size();
       }
       
       @Override
       protected boolean onTap(int index) {
          OverlayItem item = mapOverlays.get(index);
          AlertDialog.Builder dialog = new AlertDialog.Builder(context);
          dialog.setTitle(item.getTitle());
          dialog.setMessage(item.getSnippet());
          dialog.show();
          return true;
       }
       
       public void addOverlay(OverlayItem overlay) {
          mapOverlays.add(overlay);
           this.populate();
       }

    }

    
}
    
 

